﻿using CommonDevelop;
using CommonDevelop.DB;
using CommonEngine;
using CommonFP;
using ManageDB;
using ManageRepository;
using System;

namespace ManageService
{
    /// <summary>
    /// 日志服务层接口
    /// </summary>
    public interface ILogService : IBaseService<Base_Log>
    {

    }

    /// <summary>
    /// 日志服务层
    /// </summary>
    public class LogService : BaseService<Base_Log, ILogRepository>, ILogService
    {
        /// <summary>
        /// 构造注入
        /// </summary>
        public LogService(ILogRepository repository) : base(repository)
        {

        }

        public override Validation<bool> Add<TVO>(TVO En)
        {
            if (typeof(TVO).Equals(typeof(LogModel)))
            {
                LogModel result = En as LogModel;
                var entity = En.MapTo<Base_Log>();
                if (entity == null)
                    return false;

                //将视图缺失的字段补充完毕
                var i = result?.LogType.ToInt();
                entity.LogType = i.HasValue ? i.Value : 0;
                entity.LogId = Guid.NewGuid().ToString();
                return this.Add_En(entity);
            }
            else
                return base.Add<TVO>(En);
        }
    }
}
